package com.young.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.young.domain.SysRole;
import com.young.domain.SysRoleExample;
import com.young.domain.SysRoleMenu;
import com.young.domain.SysRoleMenuExample;
import com.young.domain.SysUser;
import com.young.domain.SysUserRole;
import com.young.domain.SysUserRoleExample;
import com.young.domain.SysUserRoleExample.Criteria;
import com.young.mapper.SysRoleMapper;
import com.young.mapper.SysRoleMenuMapper;
import com.young.mapper.SysUserRoleMapper;
import com.young.req.AddRoleReq;
import com.young.req.SysRolePageReq;
import com.young.req.UpdateRoleReq;
import com.young.res.RoleInfoRes;
import com.young.shop.service.SysRoleService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 角色管理服务类
 * @Author: kangyang
 * @Date: 2021/5/15 18:04
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService {

  @Autowired
  private SysRoleMapper sysRoleMapper;

  @Autowired
  private SysUserRoleMapper sysUserRoleMapper;

  @Autowired
  private SysRoleMenuMapper sysRoleMenuMapper;


  @Override
  public List<SysRole> getRoleList() {
    SysRoleExample sysRoleExample = new SysRoleExample();
    SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
    criteria.andIsDeleteEqualTo(Boolean.FALSE);
    List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
    return sysRoles;
  }

  @Override
  public List<Long> getRoleIdByUserId(Long userId) {
    List<Long> roleIds = new ArrayList<>();
    if(userId == null) {
      return roleIds;
    }
    SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
    Criteria criteria = sysUserRoleExample.createCriteria();
    criteria.andUserIdEqualTo(userId);
    criteria.andIsDeleteEqualTo(Boolean.FALSE);
    List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectByExample(sysUserRoleExample);
    if(CollectionUtils.isNotEmpty(sysUserRoles)) {
      roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId)
          .collect(Collectors.toList());
    }
    return roleIds;
  }

  @Override
  public PageInfo<SysRole> getRolePage(SysRolePageReq sysRolePageReq) {
    PageInfo<SysRole> sysRolePageInfo = new PageInfo<>();
    if(sysRolePageReq == null) {
      return sysRolePageInfo;
    }
    SysRoleExample sysRoleExample = new SysRoleExample();
    SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
    criteria.andIsDeleteEqualTo(Boolean.FALSE);
    if(StringUtils.isNotEmpty(sysRolePageReq.getRoleName())) {
      criteria.andRoleNameLike("%" + sysRolePageReq.getRoleName() + "%");
    }
    // 分页参数默认为0页，页面长度默认为10
    Integer current = 0;
    Integer size = 10;
    if(sysRolePageReq.getCurrent() != null) {
      current = sysRolePageReq.getCurrent();
    }
    if(sysRolePageReq.getSize() != null) {
      size = sysRolePageReq.getSize();
    }
    PageHelper.startPage(current, size);
    List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
    sysRolePageInfo.setList(sysRoles);

    return sysRolePageInfo;
  }

  @Override
  @Transactional
  public Boolean insertRole(AddRoleReq addRoleReq) {
    SysRole sysRole = new SysRole();
    sysRole.setRemark(addRoleReq.getRemark());
    sysRole.setRoleName(addRoleReq.getRoleName());
    sysRole.setRoleId(new Date().getTime());
    sysRole.setCreateTime(new Date());
    sysRole.setCreateUserId(addRoleReq.getCreateUserId());
    try {
      sysRoleMapper.insert(sysRole);
      List<Long> menuIdList = addRoleReq.getMenuIdList();
      if(CollectionUtils.isNotEmpty(menuIdList)) {
        menuIdList.stream().forEach(menuId -> {
          SysRoleMenu sysRoleMenu = new SysRoleMenu();
          sysRoleMenu.setRoleId(sysRole.getRoleId());
          sysRoleMenu.setMenuId(menuId);
          sysRoleMenuMapper.insert(sysRoleMenu);
        });
      }
    }catch (Exception e) {
      e.printStackTrace();
      return Boolean.FALSE;
    }

    return Boolean.TRUE;
  }

  @Override
  public RoleInfoRes getCurrentRoleByRoleId(Long roleId) {
    RoleInfoRes roleInfoRes = new RoleInfoRes();
    if(roleId == null) {
      return roleInfoRes;
    }
    SysRoleExample sysRoleExample = new SysRoleExample();
    SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
    criteria.andRoleIdEqualTo(roleId);
    criteria.andIsDeleteEqualTo(Boolean.FALSE);
    List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
    if(CollectionUtils.isNotEmpty(sysRoles)) {
      SysRole sysRole = sysRoles.get(0);
      BeanUtils.copyProperties(sysRole, roleInfoRes);
      SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
      SysRoleMenuExample.Criteria sysRoleMenuExampleCriteria = sysRoleMenuExample.createCriteria();
      sysRoleMenuExampleCriteria.andRoleIdEqualTo(roleId);
      sysRoleMenuExampleCriteria.andIsDeleteEqualTo(Boolean.FALSE);
      List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectByExample(sysRoleMenuExample);
      if(CollectionUtils.isNotEmpty(sysRoleMenus)) {
        List<Long> menuIdList = sysRoleMenus.stream().filter(s -> s != null && s.getMenuId() != null)
            .map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        roleInfoRes.setMenuIdList(menuIdList);
      }
    }
    return roleInfoRes;
  }

  @Override
  @Transactional
  public Boolean deleteRoleByRoleIds(List<Long> roleIds) {
    if(CollectionUtils.isNotEmpty(roleIds)) {
      roleIds.forEach(roleId -> {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(roleId);
        sysRole.setIsDelete(Boolean.TRUE);
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        sysRoleMenuMapper.deleteByRoleId(roleId);
        sysUserRoleMapper.deleteByUserId(roleId);
      });
    }

    return Boolean.TRUE;
  }

  @Override
  public Boolean updateRoleInfo(UpdateRoleReq updateRoleReq) {
    if(updateRoleReq != null) {
      SysRole role = new SysRole();
      BeanUtils.copyProperties(updateRoleReq, role);
      sysRoleMapper.updateByPrimaryKeySelective(role);
      List<Long> menuIdList = updateRoleReq.getMenuIdList();
      if(CollectionUtils.isNotEmpty(menuIdList)) {
        // 先删除原有的数据再新增新的数据
        sysRoleMenuMapper.deleteByRoleId(updateRoleReq.getRoleId());
        menuIdList.forEach(menuId -> {
          SysRoleMenu sysRoleMenu = new SysRoleMenu();
          sysRoleMenu.setRoleId(updateRoleReq.getRoleId());
          sysRoleMenu.setMenuId(menuId);
          sysRoleMenuMapper.insertSelective(sysRoleMenu);
        });
      }
    }

    return Boolean.TRUE;
  }
}
